﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UILabelEx : UILabel
{
    [SerializeField]
    int _textKey;
    [SerializeField]
    string textKeyName;
    LangString _langString;

    protected override void Awake()
    {
		base.Awake();
		UpdateText();
    }

//	protected override void OnEnable()
//	{
//		base.OnEnable();
//
//	}

//    public override Shader shader
//    {
//        get
//        {
//            return base.shader;
//        }
//        set
//        {
//            base.shader = value;
//        }
//    }

    public int textKey
    {
        get
        {
            return _textKey;
        }
        set
        {
            if (_textKey == value)
            {
                return;
            }

            _textKey = value;
            _langString = null;

            string str = LangManager.Get(textKey);
            if (str != null)
            {
                this.text = str;
            }
            else
            {
                this.text = "";
            }
        }
    }

    public LangString langString
    {
        get
        {
            return _langString;
        }
        set
        {
            if (_langString == value)
            {
                return;
            }

            _langString = value;
            _textKey = 0;

            if (_langString == null)
            {
                this.text = "";
                return;
            }

            string str = _langString.ToString();
            if (str != null)
            {
                this.text = str;
            }
            else
            {
                this.text = "";
            }
        }
    }

    public void UpdateText()
    {
        if (_textKey != 0)
        {
            int temp = _textKey;
            _textKey = 0;
            textKey = temp;
        }
        else if (_langString != null)
        {
            LangString temp = _langString;
            _langString = null;
            langString = temp;
        }
    }

    public void SetText(string text)
    {
        this.text = text;
        _textKey = 0;
        _langString = null;
    }

    public void SetText(int textKey)
    {
        this.textKey = textKey;
    }

    public void SetText(LangString langString)
    {
        this.langString = langString;
    }

    public void SetTextKeyName(int textKey, string keyName)
    {
        this.textKey = textKey;
        this.textKeyName = keyName;
    }

    public LangString GetLangString()
    {
        return this._langString;
    }

	public static void UpdateAll()
	{
		UILabelEx[] labels = Resources.FindObjectsOfTypeAll<UILabelEx>();
		for (int i = 0; i < labels.Length; i++)
		{
			labels[i].UpdateText();
		}
	}
}
